#include <iostream>
#include <cstring>
#include <string>
#define int long long
using namespace std;
int t, k, n, ans;
inline int read()
{
    int x = 0, f = 0;
    char ch = getchar();
    while (ch > '9' || ch < '0')
    {
        f |= (ch == '-');
        ch = getchar();
    }
    while (ch <= '9' && ch >= '0')
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return f ? -x : x;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    t = read();
    while (t--)
    {
        k = read();
        n = read();
        ans = 0;
        if (k == 1)
        {
            printf("%lld\n", n - 1);
            continue;
        }
        while (n >= k)
        {
            ans += n % k + 1;
            n /= k;
        }
        ans += n - 1;
        printf("%lld\n", ans);
    }
    return 0;
}